package com.zmn.oms.task.job.redistribute;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.common.utils.DateTimeUtil;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.services.interfaces.redistribute.OrderWorkRedistributeMasterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

/**
 * 类描述: 改派定时任务
 *
 * @author: HuangChao
 * @since: 2022/05/26 16:05
 */
@Slf4j
@LTS
public class RedistributeJob {

    @Resource
    TaskManager taskManager;
    @Autowired
    OrderWorkRedistributeMasterService orderWorkRedistributeMasterService;

    /**
     * 每天早上1点执行
     */
    @PostConstruct
    void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_DELETE_REDISTRIBUTE_MASTER_JOB);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_DELETE_REDISTRIBUTE_MASTER_JOB);
        job.setCronExpression("0 0 1 1/1 * ? ");
        job.setReplaceOnExist(true);
        ResultDTO result = taskManager.submitTask(job);
        if (!result.getStatusBool()) {
            log.error("#oms#deleteRedistributeMaster# job submit fail...");
        }
    }

    /**
     * 每天早上1点执行删除改派申请工程师任务
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_DELETE_REDISTRIBUTE_MASTER_JOB)
    public Result deleteRedistributeMasterJob(Job job) {
        log.info("#oms#deleteRedistributeMasterJob# 开始执行删除改派申请工程师任务。");
        Date date = DateTimeUtil.getAppointDay(-7);
        orderWorkRedistributeMasterService.deleteByCreateTimeAndStatus(date, GlobalConsts.NO);
        log.info("#oms#deleteRedistributeMasterJob# 开始执行删除改派申请工程师任务。删除{}之前的失效的求助工程师信息", DateUtil.dateFormatToString(date, DateUtil.FORMAT_DEFAULT));
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
